package core

import (
	"encoding/hex"
	"log"
	"math/rand"
	"bytes"
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
)

func getUUID() string {
	u := make([]byte, 16)
	_, err := rand.Read(u)
	if err != nil {
		log.Printf("[ERROR] Failed to generate GUID: %v", err)
	}

	u[8] = (u[8] | 0x80) & 0xBF
	u[6] = (u[6] | 0x40) & 0x4F

	return hex.EncodeToString(u)
}

func sendHTTPPost(target string, device *DeviceStatus) error {
	if target == "" || device == nil {
		return errors.New("Invalid inputs.")
	}

	body, err := json.Marshal(device)
	if err != nil {
		log.Printf("Failed to serialize DeviceStatus object: %v", err)
		return err
	}

	req := bytes.NewBuffer([]byte(body))
	body_type := "application/json;charset=utf-8"
	resp, _ := http.Post(target, body_type, req)
	respStr, _ := ioutil.ReadAll(resp.Body)

	log.Println(respStr)

	return nil
}